<script id="imgItem" type="text/template">
    <%#  layui.each(d.list, function(index, item){ %>
    <div class="file-item" id="<% index %>">
        <div class="thumbnail">
            <div class="file-panel">
                <i class="del-icon">移除</i>
            </div>
            <input type="hidden" class="imgs" name="img_<% index %>" value="<% d.item %>" />
            <img alt="" class="project-list-item-icon" src="<% d.prefix %><% item %>"/>
        </div>
    </div>
    <%#  }) %>
</script>
<script src="//www.luojimiaoyu.top/static/lib/editor/editor.js"></script>
<script>
    var url = '/user/submit.do';
    var __attachmentUploadUrl = '/attachment/upload.mhtml';
    var __editorHeight = 600;
    function resetHeight($){
        var body = $(window).height();
        var tHeight = $(".title-item").outerHeight(true);
        var extraHeight = body-tHeight-40;
        $(".side-panel").height(body+'px');
        $(".content-item").height(extraHeight+'px');
        var saveHeight = $(".extra-action-set").outerHeight();
        var tabHeight = $(".layui-tab-brief").height();
        var tabTitleHeight = $(".layui-tab-title").outerHeight();
        __editorHeight = extraHeight - 60 - 80;
        $("#editor-container").height(__editorHeight+'px');
        $(".layui-tab-content").height(body-saveHeight-tabTitleHeight-30+"px").css('overflow','scroll');
    }
    function showEditor(){
        $(".link-part").hide();
        $(".content-part").show();
    }
    function showLink(){
        $(".content-part").hide();
        $(".link-part").show();

    }
    //添加上传附件图
    function appendImg(imgs){
        console.log(imgs)
        //res.data.absUrl = __base.domain+res.data.url;
        layui.laytpl($("#imgItem").html()).render({'list':imgs,'prefix':__base.domain}, function(html){
            $('#thumbList').append(html);
        });
    }
    function subData (data){
        var loadIndex = layer.load(2);
        $.ajax({
            type: "post",
            url: url,
            data:data,
            success: function (res) {
                layer.close(loadIndex);
                if(res.code==0){
                    layer.alert(res.msg,function(index){
                        parent.CloseChild(true)
                    })
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(){
                layer.close(loadIndex);
            },
            complete:function (){

            }
        });
    }

    layui.use(['jquery','form', 'layedit', 'laydate','dropdown','colorpicker','laytpl'], function () {
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,dropdown = layui.dropdown
            ,colorpicker = layui.colorpicker
            ,laytpl = layui.laytpl;
        var $ = layui.jquery;
        laytpl.config({
            open: '<%',
            close: '%>'
        });
        resetHeight($);

        const { createEditor, createToolbar } = window.wangEditor
        const editorConfig = {
            placeholder: '点击这里开始编辑',
            onChange(editor) {
                //const html = editor.getHtml()

                // 也可以同步到 <textarea>
            }
        }
        const editor = createEditor({
            selector: '#editor-container',
            html: '<p><br></p>',
            config: editorConfig,
            mode: 'default', // or 'simple'
        });
        const toolbarConfig = {};
        const toolbar = createToolbar({
            editor,
            selector: '#toolbar-container',
            config: toolbarConfig,
            mode: 'default', // or 'simple'
        });

        //日期
        laydate.render({
            elem: '#post_date'
            ,type: 'datetime'
            ,isInitValue: true
        });

        //自定义事件 - hover
        dropdown.render({
            elem: '#moreAction'
            ,trigger: 'hover'
            ,data: [{
                title: '直接关闭'
                ,id: 100
            },]
        });
        //预定义颜色项
        colorpicker.render({
            elem: '#colorPicker'
            ,color: '#01aaed'
            ,predefine: true // 开启预定义颜色
        });
        $("#cancelAdd").click(function(e){
            e.preventDefault();
            parent.CloseChild(false)
        });

        form.on('select(post_type)', function(data){
            if(data.value==0){
                showEditor();
            }else{
                showLink();
            }
        });

        $(".upload-img").click(function (e){
            e.preventDefault();
            let index = layer.open({
                title: '缩略图上传',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['640px', '500px'],
                content: __attachmentUploadUrl,
            });
        })

        //监听提交
        form.on('submit(submitForm)', function (data) {
            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // })
            console.log(data.field)
            //subData(data.field)
            return false;
        });


        //表单初始赋值



    });
</script>